$(function () {

    var edit_dialog =  $('#edit_dialog');
    var edit_form =  $('#edit_form');
    var all_datagrid =  $('#all_datagrid');

    //数据表格
    all_datagrid.datagrid({
        url:'/clientResourcePool/list.do',
        striped:true,
        singleSelect:true,
        pagination:true,
        rownumbers:true,
        toolbar:"#tb",
        columns:[[
            {field:'id',hidden:true,width:100},
            {field:'client1',title:'姓名',width:100,formatter:function (value,row,index) {
                    return row.client ? row.client.name: "";
                }},
            {field:'client2',title:'最后跟踪时间',formatter:function (value,row,index) {
                    return row.client ? row.client.lastTraceTime: "";
                }},
            {field:'client3',title:'微信号',width:100,formatter:function (value,row,index) {
                    return row.client ? row.client.wechatNum: "";
                }},
            {field:'client4',title:'电话',width:100,formatter:function (value,row,index) {
                    return row.client ? row.client.tel: "";
                }},
            {field:'client5',title:'学校',width:100,formatter:function (value,row,index) {
                    return row.client ? row.client.school: "";
                }},
            {field:'client6',title:'意向校区',width:100,formatter:function (value,row,index) {
                    return row.client ? row.client.campus.name: "";
                }},
        ]]
    });
    //end


    var cmdObj = {

        assign:function(){

            $("#hide").textbox("enable");

            var row = all_datagrid.datagrid("getSelected");
            if (!row){
                $.messager.alert("温馨提示","请选择要操作的客户","info");
                return;
            }

            edit_dialog.dialog("setTitle","分配客户");

            //在读取数据之前,清除缓存
            edit_form.form("clear");

            if(row.client){
                row["aaa"]=row.client.name;
            }


            if(row.client){
                row["bbb"]=row.client.qqNum;
            }

            edit_form.form("load",row);


            edit_dialog.dialog("open");
        },
        receive:function(){

            $("#hide").textbox("disable");

            var row = all_datagrid.datagrid("getSelected");
            if (!row){
                $.messager.alert("温馨提示","请选择要操作的客户","info");
                return;
            }

            edit_dialog.dialog("setTitle","分配客户");

            //在读取数据之前,清除缓存
            edit_form.form("clear");

            if(row.client){
                row["aaa"]=row.client.name;
            }


            if(row.client){
                row["bbb"]=row.client.qqNum;
            }

            edit_form.form("load",row);


            edit_dialog.dialog("open");
        },

        //save按钮提交表单
        save : function () {
            var row = all_datagrid.datagrid("getSelected");


            var clientId = row.client.id;

            var poolId = row.id;


            //指派资源
            edit_form.form('submit', {
                url:"/client/sign.do",
                onSubmit: function(param){
                    param.clientId = clientId;
                    param.poolId = poolId;
                },

                success: function(data){
                    var data = eval('(' + data + ')');
                    if (data.success){
                        $.messager.confirm('温馨提示','分派成功',function(r){
                            edit_dialog.dialog("close");
                            all_datagrid.datagrid("load");
                        });
                    }else{
                        $.messager.alert("温馨提示",data.msg);
                    }
                }
            });
        },
        //编辑的关闭按钮功能
        cancel : function () {
            edit_dialog.dialog("close");
        },

        //刷新
        reload :function () {
            all_datagrid.datagrid("reload");
        }
    }

    //抽取优化
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");

        cmdObj[cmd]();
    })
    //end


    //dialog弹窗
    $('#edit_dialog').dialog({
        title: 'My Dialog',
        buttons:"#bb",
        closed:true,
        width: "60%",
        height: "80%"
    });
    //end

})//end